function y = DistP2Segment(P,Seg, Seg_para)

% required that Seg includes 2 distinct points
% this version is SLOW in calculation, computational expensive
% refer to calculation in note p16
% upgrade version 5.9 p4 new notes

a = Seg_para(1);   
b = Seg_para(2);

a1 = -1/a;
b1 = P(2)-a1*P(1);

xH = -(b-b1)/(a-a1);
yH = a*xH + b;

H = [xH; yH];

minx = min(Seg(1,:));
miny = min(Seg(2,:));
maxx = max(Seg(1,:));
maxy = max(Seg(2,:));

if (minx<=H(1))&&(H(1)<=maxx)&&(miny<=H(2))&&(H(2)<=maxy)
    y = norm([H(1)-P(1) H(2)-P(2)]);
else 
    dP2B = norm([Seg(1,1)-P(1) Seg(2,1)-P(2)]);
    dP2C = norm([Seg(1,2)-P(1) Seg(2,2)-P(2)]);
    y = min(dP2B, dP2C);
end

end